home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9638 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  56 lines

  1. Path: inforamp.net!ts1-10
  2. From: rmorin@inforamp.net (Randy Charles Morin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with converting string to float, please!!!
  5. Date: Sun, 03 Mar 96 07:21:51 GMT
  6. Organization: MiddleWorld SoftWare
  7. Message-ID: <4hbha6$899@sam.inforamp.net>
  8. References: <4h208l$aja@news1.sunbelt.net>
  9. NNTP-Posting-Host: ts1-10.tor.inforamp.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4h208l$aja@news1.sunbelt.net>, kberry@safefed.org (Kevin) wrote:
  13. >I need to parse up a string and perform calculations on the variables.
  14. >take this string char *Query="APR=8.9&TERM=5&AMOUNT=10000"
  15. >How would I parse it up and take out the numbers so I can use them in 
  16. floating 
  17. >point calculations?
  18. >Then I need to put them back in a string for output.
  19.  
  20. This code is far from optimized.  But the general feel is there.
  21.  
  22. main()
  23. {
  24.     yourString strAPR, strTERM, strAMOUNT;
  25.     yourDouble fAPR, fTERM, fAMOUNT;
  26.     is >> strAPR >> fAPR >> strTERM >> fTERM >> strAMOUNT >> fAMOUNT;
  27.     os << strAPR << fAPR << '&' 
  28.         << strTERM << fTERM << '&' 
  29.         << strAMOUNT << fAMOUNT;
  30. };
  31.  
  32. istream& operator >> (istream& s, yourString& strYours)
  33. {
  34.     char c;
  35.     while (1)
  36.     {
  37.         s >> c;
  38.         addcharactertoyourString(c,strYours);
  39.         if (c=='=') break;
  40.     }
  41. }
  42.  
  43. istream& operator >> (istream& s, yourDouble& fYours)
  44. {
  45.     char strTemp[64];
  46.     char c;
  47.     while (1)
  48.     {
  49.         s >> c;
  50.         if ((c=='&') || (c=='\0')) break;
  51.         addcharactertoyourString(c,strTemp);
  52.     }
  53.     fYours = ftoa(strTemp);
  54. }
  55.  
  56.